package com.itheima.securitydemo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)//开启方法注解的权限控制
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http
//            .authorizeRequests() //开启http的安全控制
//                .antMatchers("/index.html").permitAll()
//                .antMatchers("/login.html").permitAll()
////                .antMatchers("/admin/admin.html").hasRole("ADMIN")
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()//设置登陆相关的项
//                .loginPage("/login.html")
//                .loginProcessingUrl("/login") //登录请求的url地址
//                .and()
//                .csrf().disable();

        http
                .authorizeRequests()  //开启http的安全控制
                //.antMatchers("/index.html").permitAll()
                .antMatchers("/login.html").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated() //
                .and()
                .formLogin() //设置登录页相关选项
                .loginPage("/login.html")
                .loginProcessingUrl("/login") //登录请求的url地址
//                .usernameParameter("username")
//                .passwordParameter("password")
                .successHandler(new MyAuthenticationSuccessHandler())   //成功重定向
                .failureHandler(new MyAuthenticationFailUreHandler())    //失败重定向
                .and().csrf().disable()

        ;
    }

    @Autowired
    MyUserDetailsServiceImpl myUserDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(myUserDetailsService)//不能直接new，脱离了spring
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    public static void main(String[] args) {
        System.out.println("加密"+new BCryptPasswordEncoder().encode("123456"));
        System.out.println(new BCryptPasswordEncoder().upgradeEncoding("$2a$10$zHJV/h77uEyYILSoLVWCpO52s/xCATmexJYwdhw.6ENfADyzMs9SW"));

        //用户密码
        String password = "123456";
        //密码加密
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        //加密
        String newPassword = passwordEncoder.encode(password);
        System.out.println("加密密码为："+newPassword);
        //对比这两个密码是否是同一个密码
        boolean matches = passwordEncoder.matches(password, newPassword);
        System.out.println("两个密码一致:"+matches);
    }
}
